/*
 * Alex Veltsistas G00651470
 * Josh Hornsby G00721274
 * 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package proj3;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Scanner;

public class AuctionServer
{
    public static ServerSocket server;
    public static volatile boolean flag = true;
    public final static ArrayList<AuctionItem> items = new ArrayList<>();
    public final static ArrayList<ClientThread> clients = new ArrayList<>();
    
    public static AuctionItem findItem(int id)
    {
        for(int x=0; x < items.size(); x++)
        {
            if(items.get(x).getItemNumber()==id)
                return items.get(x);
        }
        return null;
    }
    public static boolean containsItem(int id)
    {
        for(int x=0; x < items.size(); x++)
        {
            if(items.get(x).getItemNumber()==id)
                return true;
        }
        return false;
    }
    public static ClientThread findUser(String name)
    {
        for(int x =0; x < clients.size(); x++)
        {
            if(clients.get(x).getUserName().equals(name))
                return clients.get(x);
        }
        return null;
    }
    public static boolean isUserNameValid(String name)
    {
        for(int x = 0; x < AuctionServer.clients.size(); x++)
        {
            ClientThread t = AuctionServer.clients.get(x);
            if(t.isAlive() && t.getUserName().equals(name))
                return false;
        }
        return true;
    }
    
    public static void main(String[] args)
    {
        int port = 5454;
        if(args.length > 0)
        {
            port = Integer.parseInt(args[0]);
            if(args.length > 1)
            {
                System.out.println("Invalid arguments only port is allowed to be specified.");
                System.exit(1);
            }
        }
        try 
        {
            server = new ServerSocket(port);
            System.out.println("Server started on port: " + port);
        } 
        catch (IOException ex) 
        {
            System.out.println("An error occured while creating the server:\n" + ex.getMessage());
        }
        new Thread()
        {
            @Override
            public void run() 
            {
                Scanner scn = new Scanner(System.in);
                String cmd = "";
                while(!cmd.toLowerCase().equals("exit"))
                {
                    cmd = scn.nextLine();
                }
                System.out.print("Server closing...");
                try 
                {
                    server.close();
                    flag = false;
                } 
                catch (IOException ex) 
                {
                    System.out.println("Error occured when closing server.");
                }
            }
        }.start();
        try
        {
            while(flag)
            {//check for clients
                if(clients.size() < 20)
                {
                    Socket clientSocket = server.accept();
                    ClientThread t = new ClientThread();
                    t.setClient(clientSocket);
                    clients.add(t);
                    t.start();
                    System.out.println("A client has connected!");
                }
                else
                {//keep checking for finished clients
                    for(int x = 0; x < clients.size(); x++)
                    {//clean-up any disconnected clients
                        if(clients.get(x).isAlive() == false)
                        {
                            clients.remove(x);
                            x--;
                        }
                    }
                }
                Thread.yield();
            }
        }
        catch (Exception ex)
        {
            if (!(ex instanceof SocketException))
                System.out.println("Error occured. Server did not close properly.");
            else
                System.out.println("DONE");
        }
        
    }
    
}